package com.xyht.sca_s.student_manage_system.modules.questionnaire.controller;


import com.xyht.sca_s.student_manage_system.auth.util.JwtTokenUtil;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.desEncodeAndDecode.annotation.SecretBody;
import com.xyht.sca_s.student_manage_system.modules.questionnaire.entity.SmsQuestionnaire;
import com.xyht.sca_s.student_manage_system.modules.questionnaire.entity.req.SmsQuestionnaireConfirmReq;
import com.xyht.sca_s.student_manage_system.modules.questionnaire.entity.req.SmsQuestionnaireReq;
import com.xyht.sca_s.student_manage_system.modules.questionnaire.service.SmsQuestionnaireService;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 投票问卷 前端控制器
 * </p>
 *
 * @author XYHT
 * @since 2023-01-29
 */
@RestController
@RequestMapping("/questionnaire")
@SecretBody
public class SmsQuestionnaireController {
    @Autowired
    private SmsQuestionnaireService smsQuestionnaireService;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @ApiModelProperty(value = "投票问卷发布")
    @PostMapping("/publish")
    public ResponseResult addPublishQuestionnaire(@RequestBody SmsQuestionnaireReq smsQuestionnaireReq, HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsQuestionnaireService.addPublishQuestionnaire(smsQuestionnaireReq, user_id);
    }

    @ApiModelProperty(value = "投票问卷提交")
    @PostMapping("/submit")
    public ResponseResult submitQuestionnaire(@RequestBody SmsQuestionnaireConfirmReq questionnaireConfirmReq, HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsQuestionnaireService.submitQuestionnaire(questionnaireConfirmReq, user_id);
    }

    @ApiModelProperty(value = "学生端投票问卷可填写查询")
    @GetMapping("/student/list")
    public ResponseResult getWebQuestionnaire(HttpServletRequest request,
                                              @RequestParam(value = "searchStr",required = false) String searchStr,
                                              @RequestParam(value = "timeoutFlag",required = false) Integer timeoutFlag,
                                              @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                              @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsQuestionnaireService.getWebQuestionnaire(user_id, pageNum, pageSize, searchStr,timeoutFlag);
    }

    @ApiModelProperty(value = "学生端投票问卷历史查询")
    @GetMapping("/student/history")
    public ResponseResult getWebQuestionnaireHistory(HttpServletRequest request,
                                                     @RequestParam(value = "searchStr",required = false) String searchStr,
                                                     @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                     @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsQuestionnaireService.getWebQuestionnaireHistory(user_id, pageNum, pageSize,searchStr);
    }

    @ApiModelProperty(value = "学生端投票问卷详情")
    @GetMapping("/student/detail")
    public ResponseResult getIncompleteQuestionnaireDetail(@RequestParam("ques_id") String ques_id,HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsQuestionnaireService.getIncompleteQuestionnaireDetail(ques_id,user_id);
    }

    @ApiModelProperty(value = "学生的本人投票问卷详情")
    @GetMapping("/student/completeDetail")
    public ResponseResult getCompleteQuestionnaireDetail(@RequestParam("ques_id") String ques_id, HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsQuestionnaireService.getCompleteQuestionnaireDetail(ques_id, user_id);
    }


    @ApiModelProperty(value = "后端获取投票问卷详情")
    @GetMapping("/back/detail")
    public ResponseResult getBackIncompleteQuestionnaireDetail(@RequestParam("ques_id") String ques_id) {
        return smsQuestionnaireService.getBackIncompleteQuestionnaireDetail(ques_id);
    }


    @ApiModelProperty(value = "后端获取用户已填写投票问卷详情")
    @GetMapping("/back/completeDetail")
    public ResponseResult getBackCompleteQuestionnaireDetail(@RequestParam("ques_id") String ques_id,
                                                             @RequestParam("user_id") String user_id) {
        return smsQuestionnaireService.getBackCompleteQuestionnaireDetail(ques_id, user_id);
    }


    @ApiModelProperty(value = "后端投票问卷列表")
    @GetMapping("/back/list")
    public ResponseResult getBackQuestionnaire(HttpServletRequest request,
                                               @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                               @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsQuestionnaireService.getBackQuestionnaire(user_id, pageNum, pageSize);
    }

    @ApiModelProperty(value = "后端投票问卷撤销")
    @PostMapping("/back/cancel")
    public ResponseResult getBackQuestionnaireCancel(HttpServletRequest request, @RequestBody SmsQuestionnaire smsQuestionnaire) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsQuestionnaireService.getBackQuestionnaireCancel(smsQuestionnaire.getId(), user_id);
    }


}

